﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace project_web2.AdminTool {
    public partial class Login : System.Web.UI.Page {
        DAL dal = new DAL();
        protected void Page_Load(object sender, EventArgs e) {

        }
        private bool checkLogin(string user, string pass) {
            bool result = false;
            
            int countUser = getUserLogin(user, pass);
            if (countUser == 1) {
                result = true;
            }
            return result;        
        }
        private int getUserLogin(string user, string pass) {
            int countUser = 0;
            string sql = "select customername from Customers where username ='" + user + "' and password ='" + pass + "'and role = 1";
            DataTable dt = dal.get_data(sql);
            countUser = dt.Rows.Count;
            return countUser;
        }
        private void loadMenu() {
            Label lb = Master.FindControl("lbUser") as Label;
            lb.Text = Session["Login"].ToString();
            Control ctr = Page.LoadControl("adminToolMenu.ascx");
            ctr.ID = "AdminToolMenu";
            ContentPlaceHolder pld = Master.FindControl("plAdminToolMenu") as ContentPlaceHolder;
            pld.Controls.Add(ctr);
        }

        protected void btnLogin_Click(object sender, EventArgs e) {
            string user = txtUsername.Text;
            
            string pass = txtPassword.Text;
            bool successLogin = checkLogin(user, pass);
            if (successLogin) {
                if (Session["Login"] == null) {
                    Session["Login"] = user;
                    lbNote.Text = "Xin chào " + Session["Login"].ToString() + "!";
                    loadMenu();
                }
            } else {
                lbNote.Text = "Login thất bại!";
            }
        }
        
    }
}